/**
 * 
 */
package com.java.test.user.controller;

import com.java.test.controller.BaseController;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.user.dto.MemberGradeInfo;
import com.java.test.user.dto.MemberInfo;
import com.java.test.user.dto.MemberSliceInfo;
import com.java.test.user.service.MemberService;
import com.java.test.util.HttpErrorCode;
import com.java.test.util.JsonResult;
import com.java.test.util.MemberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/member")
public class MemberController extends BaseController {

	@Autowired
	private MemberService memberservice;

	@RequestMapping("/getmemberList")
	public JsonResult memberList(@RequestBody MemberInfo info) {
		JsonResult js = new JsonResult();
		List<MemberInfo> list = memberservice.selectMemberList(info);
		js.setData(list);
		return js;
	}
	

	/***************************** 会员分成 ****************************/
	@RequestMapping("/sliceList")
	public JsonResult memberList() {
		JsonResult js = new JsonResult();
		List<MemberSliceInfo> list = memberservice.selectMemberSliceList();
		js.setData(list);
		return js;
	}

	@RequestMapping("/addSlice")
	public JsonResult addSlice(@RequestBody MemberSliceInfo info, HttpServletRequest request) {

		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.insertMemberSlice(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-会员分层设置-添加分层" + info.getSliceName());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;

	}

	@RequestMapping("/updateSlice")
	public JsonResult updateSlice(@RequestBody MemberSliceInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.updateMemberSliceById(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-会员分层设置-修改" + info.getSliceName());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/selectSliceByid")
	public JsonResult selectByid(@RequestBody MemberSliceInfo info) {
		JsonResult js = new JsonResult();
		MemberSliceInfo dinfo = memberservice.selectById(info.getId());
		js.setData(dinfo);
		return js;
	}

	/**
	 * 
	 * 
	 * 
	 * 
	 * 会员等级
	 * 
	 */
	@RequestMapping("/memberGradeList")
	public JsonResult memberGradeList() {
		JsonResult js = new JsonResult();
		List<MemberGradeInfo> list = memberservice.selectMemberGradeList();
		js.setData(list);
		return js;
	}

	@RequestMapping("/addGrade")
	public JsonResult addGrade(@RequestBody MemberGradeInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.addMemberGrade(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-会员等級设置-添加" + info.getMemberGradeName());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;

	}

	@RequestMapping("/updateGrade")
	public JsonResult updateGrade(@RequestBody MemberGradeInfo info, HttpServletRequest request) {
		System.out.println(MemberUtil.memberInfoMap.size());
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.updateMemberGrade(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-会员等級设置-修改" + info.getMemberGradeName());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;

	}

	@RequestMapping("/selectGradeByid")
	public JsonResult selectGradeByid(@RequestBody MemberGradeInfo info) {
		JsonResult js = new JsonResult();
		MemberGradeInfo dinfo = memberservice.selectMemberGradeInfoById(info.getId());
		js.setData(dinfo);
		return js;
	}

}
